<template>
  <div class="modal-wrapper form-limit" v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="用户：" prop="userId" key="userId">
        <span v-if="isEdit">{{ form.userId }}</span>
        <el-select v-else v-model="form.userId" placeholder="请选择用户" clearable size="small">
          <el-option v-for="(item, index) in userList" :key="index" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布限制数：" prop="userMaxCount">
        <el-input v-model="form.userMaxCount" size="small" clearable placeholder="请输入发布限制数"></el-input>
      </el-form-item>
    </el-form>
    <div class="dialog-footer form-btn-row">
      <mine-button type="primary" secondary @click="cancel">取 消</mine-button>
      <mine-button type="primary" @click="save">保 存</mine-button>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    isEdit: Boolean,
    info: {
      type: Object,
      default: function () { return {}; }
    },
  },
  data () {
    return {
      loading: false,
      form: {
        id: '',
        userId: '',
        userMaxCount: ''
      },
      rules: {
        userId: { required: true, message: '请选择用户', trigger: 'change' },
        userMaxCount: { required: true, validator: this.checkUserMaxCount, trigger: 'blur' },
      },
      userList: [],
    };
  },
  methods: {
    checkUserMaxCount (rule, value, callback) {
      if (!value && value !== 0) {
        callback(new Error('请输入发布限制数'));
      } else if (!this.$regex.isNumber.test(value)) {
        callback(new Error('请输入自然数'));
      } else {
        callback();
      }
    },
    show () {
      this.$refs.form.resetFields();
      this.getUserList();
      if (this.isEdit) {
        this.form = {
          ...this.info
        };
      }
    },
    async save () {
      if (!this.$common.doSubmit('form', this)) return;
      const params = [{
        userId: this.form.userId,
        userMaxCount: this.form.userMaxCount,
      }];
      if (this.isEdit) {
        params[0].id = this.form.id;
      }
      this.loading = true;
      try {
        const res = await this.$store.dispatch('mpaAllowCountBatchSave', params);
        const result = res.data || {};
        if (result.success) {
          this.$message({ type: 'success', message: result.msg || '保存成功' });
          this.$emit('submit');
          this.$emit('close');
        } else {
          this.$message({ type: 'error', message: result.msg || '保存失败' });
        }
      } catch (e) {
        console.error(e);
      }
      this.loading = false;
    },
    // 获取用户列表
    async getUserList () {
      this.loading = true;
      try {
        const res = await this.$store.dispatch('userSearch');
        const result = res.data || {};
        if (result.success) {
          const data = result.data && result.data.length ? result.data : [];
          this.userList = data.map(item => {
            return {
              id: item.userName,
              name: item.userName
            };
          });
        } else {
          this.userList = [];
        }
      } catch (e) {
        console.error(e);
      }
      this.loading = false;
    },
    cancel () {
      this.$emit('close');
    },
  },
  created () {

  }
}
</script>
